package com.easyapi.service;

import com.alibaba.druid.pool.DruidDataSource;
import com.easyapi.common.bean.ApiCenterDatasource;
import com.easyapi.common.emun.DataSourceStatus;
import com.easyapi.common.emun.DatasourceOperationEnum;
import com.easyapi.common.emun.SqlDriverClassName;
import com.easyapi.common.emun.StatusCode;
import com.easyapi.common.exception.BizException;
import com.easyapi.mapper.ApiCenterDatasourceMapper;
import com.easyapi.utils.CaffeineCacheUtil;
import com.easyapi.utils.DruidDataSourceUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author: javadadi
 * @Time: 20:17
 * @ClassName: DatasourceService
 */
@Service
public class DatasourceService {
    @Resource
    private ApiCenterDatasourceMapper datasourceMapper;

    /**
     * 添加数据源
     * @param id datasourceId
     */
    public void addDatasource(Long id)  {
        if(id<=0){
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        ApiCenterDatasource datasource = datasourceMapper.selectById(id);
        if(Objects.isNull(datasource)){
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        DruidDataSource druidDataSource = DruidDataSourceUtil.getDruidDataSource(datasource.getDataJson(),
                datasource.getDatasourceUsername(),
                datasource.getDatasourcePassword(),
                SqlDriverClassName.MYSQL.getDriverClassName());
        if(Objects.isNull(druidDataSource)){
            datasource.setStatus(DataSourceStatus.LINK_FAIL.getCode());
        }
        CaffeineCacheUtil.add(datasource.getDataName()+datasource.getId(),datasource);
    }

    public void updateDatasource(Long id)  {
        if(id<=0){
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        ApiCenterDatasource datasource = datasourceMapper.selectById(id);
        if(Objects.isNull(datasource)){
            throw new BizException(StatusCode.NO_SUCH_DATASOURCE);
        }
        DruidDataSource druidDataSource = DruidDataSourceUtil.getDruidDataSource(datasource.getDataJson(),
                datasource.getDatasourceUsername(),
                datasource.getDatasourcePassword(),
                SqlDriverClassName.MYSQL.getDriverClassName());
        if(Objects.isNull(druidDataSource)){
            datasource.setStatus(DataSourceStatus.LINK_FAIL.getCode());
        }
        CaffeineCacheUtil.add(datasource.getDataName()+datasource.getId(),datasource);
    }

    public void deleteDatasource(Long id)  {
        if(id<=0){
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        ApiCenterDatasource datasource = datasourceMapper.selectById(id);
        if(Objects.isNull(datasource)){
            throw new BizException(StatusCode.NO_SUCH_DATASOURCE);
        }
        if(CaffeineCacheUtil.hasCache(datasource.getDataName()+datasource.getId())){
            CaffeineCacheUtil.remove(datasource.getDataName()+datasource.getId());
        }
    }

    public void execute(String action,Long id)  {
        if(id<=0){
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        if(DatasourceOperationEnum.ADD.getAction().equals(action)){
            addDatasource(id);
        }else if(DatasourceOperationEnum.UPDATE.getAction().equals(action)){
            updateDatasource(id);
        } else if(DatasourceOperationEnum.DELETE.getAction().equals(action)){
            deleteDatasource(id);
        }
    }
}
